#include "read_boy_confg.h"
#include "read_boy_book_epub.h"
#include "read_boy_book.h"
#include "read_boy_date_time.h"
#include "RX8025T.h"

void read_boy_book_init_set_Table_of_epub_Contents(){//更新EPUB电子书目录
    
    FRESULT res;  // FatFs返回结果
    DIR dir;      // 目录对象
    FILINFO fno;  // 文件信息结构体
    
    FIL file;  // 文件对象
    UINT bw;
    int i = 0;
    
    res = f_open(&file,  BOOK_PATH_EPUB_NEWFILE_TXT, FA_READ);
    if (res == FR_OK) { 
        f_close(&file); 
        //delete_file(BOOK_PATH_NEWFILE_TXT);
        // 删除文件
        res = f_unlink(BOOK_PATH_EPUB_NEWFILE_TXT);
        if (res != FR_OK) {
            //return res; // 如果删除失败，返回错误码
        }
        
    }
    
    // 尝试打开目录
    res = f_opendir(&dir,  BOOK_PATH_EPUB_FOLDER);
    if (res != FR_OK) {
        //printf("打开目录失败，错误码：%d\n", res);
        return;
    }
    
     // 以写入模式打开文件，如果不存在则创建
    if (f_open(&file, BOOK_PATH_EPUB_NEWFILE_TXT, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) {
        //error("File open failed");
        return;
    }
    
    draw_string_for_sd_start();  
     // 循环读取目录项
    while (1) {
        res = f_readdir(&dir, &fno);  // 读取一个目录项
        if (res != FR_OK || fno.fname[0] == 0) break;  // 出错或到达目录尾部时退出循环
        if (fno.fattrib & AM_DIR) {
            // 这是一个目录
            //draw_string(0,0,16,fno.fname);
             
            draw_string_for_sd(my_read_boy_book.iX_initial_value +30,my_read_boy_book.iY_initial_value+ 30*i,24,fno.fname);
            
            res = f_lseek(&file, i * 40);
            if (res == FR_OK) {
                //const char *data = "追加的数据\n";
                res = f_write(&file, fno.fname, 40, &bw); // 写入数据
                f_sync(&file); // 刷新数据
            }
            
            i++;
        }
        
    }
    READ_BOY_BOOK_DISP;
            
    draw_string_for_sd_end();
    f_closedir(&dir);  // 关闭目录
    if (f_close(&file) != FR_OK) {
        //error("File close failed");
        return;
    }
}

typedef struct epub{
    uint16_t contents_index;
    uint16_t book_total_count;
    uint16_t opf_file_num;
    char epub_dir[48];
    char opf_name[60];
    char opf_dir[48];
    char href_file_name[32];
}epub_t;
epub_t my_epub;

void my_epub_book_total_count_init(){
    //FIL file;  // 文件对象
    FRESULT res;  // FatFs返回结果
    FILINFO fno;  // 文件信息结构体
    
    //res = f_open(&file,  BOOK_PATH_EPUB_NEWFILE_TXT, FA_READ);
    //if (res == FR_OK) {
        // 获取文件状态
        res = f_stat(BOOK_PATH_EPUB_NEWFILE_TXT, &fno);
        if (res == FR_OK) {
            // 如果成功，将文件大小存储在提供的指针中
            my_epub.book_total_count = fno.fsize / 40;
        }//
        //f_close(&file) ;  
    //}
}

void my_epub_init(){
    my_epub.contents_index = 0;
    my_epub.book_total_count = 0;
    my_epub_book_total_count_init();
    my_epub.opf_file_num = 1;
}
void show_epub_Contents(book_contents_t *contents){
    FRESULT res;  // FatFs返回结果
    FIL file;  // 文件对象
    char out_buff[40];
    uint32_t rd_len; 
    
    char contents_start ; 
    char contents_end ; 
    char contents_num ; 
    
    contents_num = my_epub.contents_index % contents->lines;
    contents_start =  my_epub.contents_index / contents->lines * contents->lines;
    contents_end = my_epub.contents_index / contents->lines + 1;
    if(contents_end == contents->page){
        contents_end = my_epub.book_total_count;
    }else{
        contents_end = contents_start + contents->lines;
    }
    
    
    
    draw_string_for_sd_start( ); 
    res = f_open(&file,  BOOK_PATH_EPUB_NEWFILE_TXT, FA_READ);
    if (res == FR_OK) {
        
        for(int i = contents_start; i < contents_end; i++){
            res = f_lseek(&file, i*40);
            if (res == FR_OK) {
                res = f_read(&file, out_buff, 40, &rd_len);
                draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (i-contents_start), 24,out_buff);

            }
        }        
    }
    f_close(&file) ;
    draw_string_for_sd_end(); 
    read_boy_draw_char_en(my_read_boy_book.iX_initial_value + 10  ,my_read_boy_book.iY_initial_value + contents_num*24,read_boy_FONT_16x24, '>',0x0);
    
}
char read_boy_book_show_epub_Contents(){//显示目录
    static char flag = 0;
    char key_num;
    
    my_epub_init();
    book_contents_t my_book_contents;
    my_book_contents.lines = my_read_boy_book.hight / 24;
    my_book_contents.page  = my_epub.book_total_count / my_book_contents.lines;
    if(my_epub.book_total_count % my_book_contents.lines){
        my_book_contents.page  += 1;
    }
    
    while(1){
        key_num = readboy_key_scan();
        switch(flag){
            case 0:
                READ_BOY_BOOK_DISP_CLEAR; 
                flag = 1;
            //break;
            case 1:
                //read_boy_book_init_set_Table_of_epub_Contents();
                show_epub_Contents(&my_book_contents);
                READ_BOY_BOOK_DISP; 
                flag = 2;
            break;
            case 2:
                if(key_num == 1){
                    //my_read_boy_book.old_index = my_read_boy_book.new_index;
                    my_epub.contents_index++;// = (my_read_boy_book.new_index + 1) % 2;
                    my_epub.contents_index = my_epub.contents_index % my_epub.book_total_count;
                    flag = 0;
                }
                if(key_num == 2){
                    flag = 0;
                    return 2;
                }
                
            break;
                
        }
    }
    return 0;
}

//==============================
void my_epub_dir_init(){
    FRESULT res;  // FatFs返回结果
    FIL file;  // 文件对象
    char out_buff[40];
    uint32_t rd_len; 
    
    res = f_open(&file,  BOOK_PATH_EPUB_NEWFILE_TXT, FA_READ);
    if (res == FR_OK) {
        res = f_lseek(&file, my_epub.contents_index*40);
        if (res == FR_OK) {
            res = f_read(&file, out_buff, 40, &rd_len);
            //draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (i-contents_start), 24,out_buff);

        }
    }
    f_close(&file) ;
    
    strcpy(my_epub.epub_dir,BOOK_PATH_EPUB_FOLDER);
    //strcpy(&my_epub.epub_dir[6],out_buff);
    strcat(my_epub.epub_dir, "/");
    strcat(my_epub.epub_dir, out_buff);
    
    
}

// 递归统计文件夹大小  查找opf文件
FRESULT get_folder_size(const char* path) {
    DIR dir;
    FILINFO fno;
    FRESULT res;

    // 打开目录
    res = f_opendir(&dir, path);
    if (res != FR_OK) {
        return res;  // 打开目录失败
    }

    // 遍历目录
    while (1) {
        res = f_readdir(&dir, &fno);
        if (res != FR_OK || fno.fname[0] == 0) {
            break;  // 遍历结束
        }

        // 跳过 "." 和 ".."
        if (fno.fname[0] == '.' && (fno.fname[1] == '\0' || (fno.fname[1] == '.' && fno.fname[2] == '\0'))) {
            continue;
        }

        // 如果是文件，累加大小
        if (!(fno.fattrib & AM_DIR)) {
            //*total_size += fno.fsize;
            char *str;
            str = strstr(fno.fname,".opf");
            if(str != NULL){
                
                strcpy(my_epub.opf_name,path);   
                strcat(my_epub.opf_name, "/");
                strcat(my_epub.opf_name, fno.fname);
                
                strcpy(my_epub.opf_dir,path);   
                strcat(my_epub.opf_dir, "/");
                
                break;;
            }
        }
        // 如果是子目录，递归统计
        else {
            char sub_path[256];
            //sprintf(sub_path, "%s/%s", path, fno.fname);
            
            strcpy(sub_path,path);   
            strcat(sub_path, "/");
            strcat(sub_path, fno.fname);
            
            res = get_folder_size(sub_path);
            if (res != FR_OK) {
                f_closedir(&dir);
                return res;  // 递归调用失败
            }
        }
    }

    // 关闭目录
    f_closedir(&dir);
    return FR_OK;
}

void read_opf_file(){
    FILINFO fno;
    FRESULT res;
    FIL file;  // 文件对象
    uint32_t rd_len; 
    uint8_t data[512];
    
    res = f_stat(my_epub.opf_name, &fno);
    if (res == FR_OK) {
        // 如果成功，将文件大小存储在提供的指针中
        if(fno.fsize <=1024){
            res = f_open(&file,  my_epub.opf_name, FA_READ);
            if (res == FR_OK) {
                res = f_read(&file, data, fno.fsize, &rd_len);
                
                draw_string_for_sd_start( ); 
                data[40] = 0;
                draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (0), 24,data);
                draw_string_for_sd_end(); 
            }
            f_close(&file) ;
        }else{
            res = f_open(&file,  my_epub.opf_name, FA_READ);
            if (res == FR_OK) {
                res = f_lseek(&file, 1024);
                res = f_read(&file, data, 1024, &rd_len);
                char mydata[20];
                char *str = strstr(data,"href");
                str = strstr(str+4,"href");
                strncpy(mydata,str,19);
                mydata[19] = 0;
                draw_string_for_sd_start( ); 
                
                draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (0), 24,mydata);
                draw_string_for_sd_end(); 
            }
            f_close(&file) ;
        }
    }
}

char find_href_file_name(char *str,char* name){
    char* str1;
    char* str2;
    str1 = strstr(str,"\"");
    str2 = strstr(str1+1,"\"");
    strncpy(name,str1+1,str2-str1-1);
    name[str2-str1-1] = 0;
    return 0;
}

void read_opf_file_name(){
    FILINFO fno;
    FRESULT res;
    FIL file;  // 文件对象
    uint32_t rd_len; 
    char data[512];
    uint32_t lseek = 0;
    char *str;
    
    char href_file_name[32];
    //uint16_t href_num = 0;
    
    res = f_stat(my_epub.opf_name, &fno);
    if (res == FR_OK) {
        // 如果成功，将文件大小存储在提供的指针中
        //if(fno.fsize <=1024){
            
        res = f_open(&file,  my_epub.opf_name, FA_READ);
        if (res == FR_OK) {
            while(1){
                res = f_lseek(&file, lseek);
                res = f_read(&file, data, 512, &rd_len);
                
                str = strstr(data,"href");
                if(str != NULL){
                    //href_num++;
                    //if(href_num == num){//读取文件名
                        res = f_lseek(&file, lseek + (str - data));
                        res = f_read(&file, data, 100, &rd_len);
                        find_href_file_name(data,href_file_name);
                        char *name = strstr(href_file_name,my_epub.href_file_name);
                        if(name != NULL){
                            strcpy(my_epub.href_file_name,href_file_name);
                            
//                            draw_string_for_sd_start( );            
//                            draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (1), 24,my_epub.href_file_name);
//                            draw_string_for_sd_end(); 
                            
                            f_close(&file) ;
                            return;
                        }
                        
                        //data[40] = 0;
                        
                    //}
                    while(1){
                        str = strstr(str+4,"href");
                        if(str != NULL){
                            //href_num++;
                            //if(href_num == num){//读取文件名
                                res = f_lseek(&file, lseek + (str - data));
                            res = f_read(&file, data, 100, &rd_len);
                            find_href_file_name(data,href_file_name);
                            char *name = strstr(href_file_name,my_epub.href_file_name);
                            if(name != NULL){
                                strcpy(my_epub.href_file_name,href_file_name);
                                
//                                draw_string_for_sd_start( );            
//                                draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (1), 24,my_epub.href_file_name);
//                                draw_string_for_sd_end(); 
                                
                                f_close(&file) ;
                                return;
                            }
                            //}
                        }else{
                            break;
                        }
                    }
                    
                }
                lseek = lseek + 512 -3;
                if(lseek > fno.fsize){
                    break;
                }
                
            }
            
        
            f_close(&file) ;
        }
    }
}

void read_opf_file_name_idref(uint16_t num){
    FILINFO fno;
    FRESULT res;
    FIL file;  // 文件对象
    uint32_t rd_len; 
    char data[512];
    uint32_t lseek = 0;
    char *str;
    uint16_t idref_num = 0;
    
    res = f_stat(my_epub.opf_name, &fno);
    if (res == FR_OK) {
        res = f_open(&file,  my_epub.opf_name, FA_READ);
        if (res == FR_OK) {
            while(1){
                res = f_lseek(&file, lseek);
                res = f_read(&file, data, 512, &rd_len);
                
                str = strstr(data,"idref");
                if(str != NULL){
                    idref_num++;
                    if(idref_num == num){//读取文件名
                        res = f_lseek(&file, lseek + (str - data));
                                res = f_read(&file, data, 100, &rd_len);
                                find_href_file_name(data,my_epub.href_file_name);
                                
                                //data[40] = 0;
//                                draw_string_for_sd_start( );            
//                                draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (0), 24,my_epub.href_file_name);
//                                draw_string_for_sd_end(); 
                    }
                    while(1){
                        str = strstr(str+5,"idref");
                        if(str != NULL){
                            idref_num++;
                            if(idref_num == num){//读取文件名
                                res = f_lseek(&file, lseek + (str - data));
                                res = f_read(&file, data, 100, &rd_len);
                                find_href_file_name(data,my_epub.href_file_name);
                                
                                //data[40] = 0;
//                                draw_string_for_sd_start( );            
//                                draw_string_for_sd( my_read_boy_book.iX_initial_value + 40 ,my_read_boy_book.iY_initial_value + 24 * (0), 24,my_epub.href_file_name);
//                                draw_string_for_sd_end(); 
                            }
                        }else{
                            break;
                        }
                    }
                    
                }
                lseek = lseek + 512 -4;
                if(lseek > fno.fsize){
                    break;
                }
            }
            f_close(&file);
        }
    }
}

//========================================
//查找<
int find_right_V(const char *str){
    int num = 0;
    while(1){
        if(str[num] == '<'){
            break;
        }
        num++;
        if(num > 10000){
            return 0;
        }
    }
    return num;
}

int find_left_V(const char *str){
    int num = 0;
    while(1){
        if(str[num] == '>'){
            break;
        }
        num++;
        if(num > 10000){
            return 0;
        }
    }
    return num;
}

uint16_t utf8_to_gbk(uint8_t x1,uint8_t x2,uint8_t x3);
void utf_8_to_gbk_str(unsigned char *str){
    int num = 0;
    int gbk_num = 0;
    uint16_t data_temp;
    //char data[100];
    unsigned char *pt = str;
    while( pt[num]){
        if(pt[num] <= 0x7f){
            pt[gbk_num] = pt[num];
            num++;
            gbk_num++;
        }else{
            if(pt[num] <= 0xD1){
                pt[gbk_num] = '-';
                pt[gbk_num + 1] = '-';
                num+=2;
                gbk_num+=2;
            }else{
                data_temp = utf8_to_gbk(pt[num],pt[num+1],pt[num+2]); 
                if(data_temp == 0){
                    pt[gbk_num] = '.';
                    pt[gbk_num + 1] = '.';
                }else{
                    pt[gbk_num] = data_temp / 256;
                    pt[gbk_num + 1] = data_temp % 256;
                }
                num+=3;
                gbk_num+=2;
            }
        }
    }
    pt[gbk_num] = 0;
    
}
void show_h1(const char *str){
    char data[100];
    int num;
    str += 4;
    num = find_right_V( str);
    memcpy(data,str,num);
    data[num] = '\n';
    data[num + 1] = 0;
    utf_8_to_gbk_str(data);
    
  
    draw_epub_book("        ");     
    draw_epub_book(data);
    
    
}


void show_epub_h1(char* name){
    FILINFO fno;
    FRESULT res;
    FIL file;  // 文件对象
    uint32_t rd_len; 
    char data[1024];
    uint32_t lseek = 0;   
    char *h1;
    
    res = f_stat(name, &fno);
    if (res == FR_OK) {
        res = f_open(&file,name, FA_READ);
        if (res == FR_OK) {
            while(1){
                res = f_lseek(&file, lseek);
                res = f_read(&file, data, 1024, &rd_len);
                h1 = strstr(data,"<h1>");
                if(h1 != NULL){
                    res = f_lseek(&file, lseek + h1 - data );
                    res = f_read(&file, data, 512, &rd_len);
                    show_h1(data);
                    f_close(&file);
                    return;
                }
                lseek = lseek + 1024 -3;
            }            
            f_close(&file);
        }
    }
}

void show_epub_h_n(char* name,char* hn){
    FILINFO fno;
    FRESULT res;
    FIL file;  // 文件对象
    uint32_t rd_len; 
    char data[1024];
    uint32_t lseek = 0;   
    char *h1;
    
    res = f_stat(name, &fno);
    if (res == FR_OK) {
        res = f_open(&file,name, FA_READ);
        if (res == FR_OK) {
            while(1){
                res = f_lseek(&file, lseek);
                res = f_read(&file, data, 1024, &rd_len);
                if (res != FR_OK || rd_len == 0) {
                    // 读取失败或到达文件末尾
                    break;
                }
                //h1 = strstr(data,"<h1>");
                h1 = strstr(data,hn);
                if(h1 != NULL){
                    res = f_lseek(&file, lseek + h1 - data );
                    res = f_read(&file, data, 512, &rd_len);
                    show_h1(data);
                    f_close(&file);
                    return;
                }
                lseek = lseek + 1024 -3;
            }            
            f_close(&file);
        }
    }
}
//=============================================
char show_Vp(const char *str ){
    int num = 0;
    char key_num;
    //char Vp_data[512];
    char* Vp_data;
    
    str += 2;
    while(1){
       num = find_left_V( str);
        str += (num+1);
       if(str[0] == '<'){
       
       }else{
            break;
       }     
    }
    
    num = find_right_V( str);
    
    //memcpy(Vp_data,str,num);
    Vp_data = str  ;
    //data[num] = 0;
    Vp_data[num] = '\n';
    Vp_data[num + 1] = 0;
    utf_8_to_gbk_str(Vp_data);
    
    key_num = draw_epub_book(Vp_data); 
    if(key_num == 2){
        return key_num;
    }
    //draw_string_for_sd(50  ,16 + 16 * y_num, 16,data,RGB565_GREEN);
    return 0;
     
}
char show_epub_VP(char* name){
    FILINFO fno;
    FRESULT res;
    FIL file;  // 文件对象
    uint32_t rd_len; 
    char data[1025];
    uint32_t lseek = 0;   
    char *vp;
    char key_num;
    
    char Vp_data[512];
    
    res = f_stat(name, &fno);
    if (res == FR_OK) {
        res = f_open(&file,name, FA_READ);
        if (res == FR_OK) {
            while(1){
                res = f_lseek(&file, lseek);
                res = f_read(&file, data, 1024, &rd_len);
                if (res != FR_OK || rd_len == 0) {
                    // 读取失败或到达文件末尾
                    break;
                }
                data[rd_len] = 0;
                vp = strstr(data,"<p");
                if(vp != NULL){
                    res = f_lseek(&file, lseek + vp - data );
                    res = f_read(&file, Vp_data, 512, &rd_len);
                    key_num = show_Vp(Vp_data);
                    if(key_num == 2){
                        f_close(&file);
                        return 2;
                    }
                    while(1){
                        if((1024 -(vp - data)) < 4){
                            break;
                        }
                        vp = strstr(vp+2,"<p");
                        if(vp != NULL){
                            res = f_lseek(&file, lseek + vp - data );
                            res = f_read(&file, Vp_data, 512, &rd_len);
                            key_num = show_Vp(Vp_data);
                            if(key_num == 2){
                                f_close(&file);
                                return 2;
                            }
                        }else{
                            break;
                        }
                    }
                    //f_close(&file);
                    //return;
                }
                lseek = lseek + 1024 -1;
                //if(lseek >= fno.fsize){//file.filesize
//                if(lseek >= file.){//file.filesize    
//                    break;
//                }
            }            
            f_close(&file);
        }
    }
    return 0;
}

char show_epub_book(){
    char name[80];
    char key_num;
    strcpy(name,my_epub.opf_dir);
    strcat(name,my_epub.href_file_name);
    
    draw_string_for_sd_start( );   
    //show_epub_h1(name);
    show_epub_h_n(  name,"<h1>");
    show_epub_h_n(  name,"<h2>");
    show_epub_h_n(  name,"<h3>");
    show_epub_h_n(  name,"<h4>");
    show_epub_h_n(  name,"<h5>");
    show_epub_h_n(  name,"<h6>");
    
    key_num = show_epub_VP(name);
    
    draw_string_for_sd_end();
    
    return key_num;
}
char read_boy_book_show_epub_book(){
    char key_num;
    static char flag = 0;
    
    my_epub_dir_init();
    get_folder_size(my_epub.epub_dir);
    
    while(1){
        key_num = readboy_key_scan();
        switch(flag){
            case 0:
                READ_BOY_BOOK_DISP_CLEAR; 
                flag = 1;
            //break;
            case 1:
                //read_opf_file();
                //read_opf_file_name(3);
                read_opf_file_name_idref(my_epub.opf_file_num);
                read_opf_file_name();
                //READ_BOY_BOOK_DISP; 
                flag = 2;
            break;
            case 2:
                key_num = show_epub_book();
                if(key_num == 2){
                    flag = 0;
                    return key_num;
                }
                READ_BOY_BOOK_DISP; 
                
                flag = 3;
            break;
            case 3:
                my_epub.opf_file_num++;
                flag = 0;
            break;
        }
    }
    return 0;
}

char draw_book_page(){
    char key;
    if((my_read_boy_book.iY - my_read_boy_book.iY_initial_value) >  (my_read_boy_book.hight - my_read_boy_book.line_hight)){
        my_read_boy_book.iY = my_read_boy_book.iY_initial_value;//================================
        READ_BOY_BOOK_DISP; 
        while(1){
            key = readboy_key_scan();
            if(key == 1){
                READ_BOY_BOOK_DISP_CLEAR; 
                break;
            }
            if(key == 2){
                READ_BOY_BOOK_DISP_CLEAR; 
                return 2;
            }
        }
    } 
    return 0;
}
char draw_epub_book(char *str){
    static char flag = 0;
    char key_num;
    switch(flag){
        case 0:

            flag = 1;
        //break;
        case 1:
            while(*str){
                if(*str <= 0x7f){
                    if((my_read_boy_book.iX - my_read_boy_book.iX_initial_value) >= (my_read_boy_book.width - my_read_boy_book.en_font_x)){
                        my_read_boy_book.iY += my_read_boy_book.line_hight;
                        my_read_boy_book.iX = my_read_boy_book.iX_initial_value;//============================
                        key_num = draw_book_page();
                        if(key_num == 2){
                            return key_num;
                        }
                    }
                    if(*str == '\n'){
                        my_read_boy_book.iY += my_read_boy_book.line_hight;
                        my_read_boy_book.iX = my_read_boy_book.iX_initial_value;//============================
                        key_num = draw_book_page();
                        if(key_num == 2){
                            return key_num;
                        }
                    }else{
                        read_boy_draw_char_en(my_read_boy_book.iX,my_read_boy_book.iY,my_read_boy_book.EN_font, *str,0);
                    }
                    my_read_boy_book.iX += my_read_boy_book.en_font_x;
                    str++;
                    //my_epub_book.lines_num++;
                }else{

                    if((my_read_boy_book.iX - my_read_boy_book.iX_initial_value) >= (my_read_boy_book.width - my_read_boy_book.cn_font_x)){
                        //break;
                        my_read_boy_book.iY += my_read_boy_book.line_hight;
                        my_read_boy_book.iX = my_read_boy_book.iX_initial_value;//============================
                        key_num = draw_book_page();
                        if(key_num == 2){
                            return key_num;
                        }
                    }

                    char hz[3];
                    hz[0] = str[0];
                    hz[1] = str[1];
                    hz[2] = 0;
                
                    draw_string_for_sd(my_read_boy_book.iX,my_read_boy_book.iY,24,hz); 
                    my_read_boy_book.iX += my_read_boy_book.cn_font_x;    
                    str+=2;
                    //my_epub_book.lines_num+=2;
                }
            
            }
        break;
    }
    return 0;
}






